Fix segfault writing ascii .vtu files with implicit cell type arrays
authorAnton Gladky <gladk@debian.org>
Tue, 28 Jul 2026 14:20:19 +0000 (16:20 +0200)
committerAnton Gladky <gladk@debian.org>
Tue, 28 Jul 2026 14:20:19 +0000 (16:20 +0200)
Forwarded: not-yet

NewIterator() returns nullptr for the implicit cell-type arrays used
since VTK 9.6, so WriteAsciiData() dereferenced that nullptr in
iter->Delete(). Merely guarding the Delete() is not enough: the
templated writer already returns early on a null iterator, so the array
is then written empty, and since WriteInlineData() discards the return
value the writer reports success for a file which has lost all of its
cells. Write such arrays through an explicit copy instead.
Closes yade FTBFS.

Gbp-Pq: Name fix_xmlwriter_ascii_implicit_cellarray_segfault.patch

IO/XML/vtkXMLWriter.cxx

index 413f0f8852d44429219d57e463b895986b3e0061..c56e264160015af4445bcae774a5d4583bcb7018 100644 (file)
@@ -34,6 +34,7 @@
 #include "vtkOutputStream.h"
 #include "vtkPointData.h"
 #include "vtkPoints.h"
+#include "vtkSmartPointer.h"
 #include "vtkStdString.h"
 #include "vtkStreamingDemandDrivenPipeline.h"
 #include "vtkStringFormatter.h"
@@ -1968,6 +1969,15 @@ int vtkXMLWriteAsciiData(ostream& os, iterT* iter, vtkIndent indent)
 int vtkXMLWriter::WriteAsciiData(vtkAbstractArray* a, vtkIndent indent)
 {
   vtkArrayIterator* iter = a->NewIterator();
+  vtkSmartPointer<vtkAbstractArray> copy;
+  if (!iter)
+  {
+    // Arrays which provide no iterator, such as the implicit arrays used since VTK 9.6
+    // for uniform cell types, are written through an explicit copy.
+    copy.TakeReference(vtkAbstractArray::CreateArray(a->GetDataType()));
+    copy->DeepCopy(a);
+    iter = copy->NewIterator();
+  }
   ostream& os = *(this->Stream);
   int ret;
   switch (a->GetDataType())
@@ -1978,7 +1988,10 @@ int vtkXMLWriter::WriteAsciiData(vtkAbstractArray* a, vtkIndent indent)
       ret = 0;
       break;
   }
-  iter->Delete();
+  if (iter)
+  {
+    iter->Delete();
+  }
   return ret;
 }